﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System.Web.Mvc;
	using Falcon.Core;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain.Services;

	public class ForumAnnouncementUpdate : ValueBase<ForumAnnouncementUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumAnnouncementUpdate"/> class.
		/// </summary>
		public ForumAnnouncementUpdate()
		{
			this.ForumService = Application.Resolve<IForumService>();
		}

		public ForumAnnouncementUpdate(ForumAnnouncement announcement) 
			: this()
		{
			this.Id = announcement.Id;
			this.ForumId = announcement.ForumId;
			this.Title = announcement.Title;
			this.Body = announcement.Body;
			this.Start = announcement.Start.ToString("M/d/yyyy");
			this.Expire = announcement.Expire.ToString("M/d/yyyy");
		}

		/// <summary>
		/// Gets or sets the forum repository.
		/// </summary>
		/// <value>The forum repository.</value>
		public IForumService ForumService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the forum id.
		/// </summary>
		/// <value>The forum id.</value>
		public int ForumId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the start.
		/// </summary>
		/// <value>The start.</value>
		public string Start
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the expire.
		/// </summary>
		/// <value>The expire.</value>
		public string Expire
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the forums.
		/// </summary>
		/// <value>The forums.</value>
		public SelectList Forums
		{
			get
			{
				var forums = ForumService.GetAll(new ForumSpecification { SkipPermissions = true });

				forums.Add(new Forum { Title = ForumsResources.Titles.GlobalAnnouncement });

				return new SelectList(forums, "Id", "Title", this.ForumId);
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<ForumAnnouncementUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(ForumsResources.Titles.Announcement));

			rules.Add(x => x.Start)
				.NotEmpty(Resources.Errors.Required.Fmt(ForumsResources.Titles.StartDate));

			rules.Add(x => x.Start)
				.Date(Resources.Errors.DateNotValid.Fmt(ForumsResources.Titles.StartDate));

			rules.Add(x => x.Expire)
				.NotEmpty(Resources.Errors.Required.Fmt(ForumsResources.Titles.EndDate));

			rules.Add(x => x.Expire)
				.Date(Resources.Errors.DateNotValid.Fmt(ForumsResources.Titles.EndDate));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}